package com.li.manage.action;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.li.commons.util.ImageUtils;
import com.li.commons.util.StringUtils;
import com.li.manage.enums.UserStateEnum;
import com.li.manage.model.Role;
import com.li.manage.model.User;
import com.li.manage.query.RoleQuery;
import com.li.manage.query.UserQuery;
import com.li.manage.service.RoleService;
import com.li.manage.service.UserService;

/**
 * 角色
 * @author liwm
 *
 */
@Controller
@RequestMapping("/role")
public class RoleAction{

	@Autowired
	private RoleService roleService;
	
	/**
	 * 列表
	 * /user/userList
	 * @param req
	 * @return
	 */
	@RequestMapping("/roleList")
	public ModelAndView roleList(HttpServletRequest req,RoleQuery roleQuery){
		ModelAndView mav = new ModelAndView();
		List<Role> roleList = roleService.getRoleList(roleQuery);
		mav.addObject("roleList", roleList);
		mav.setViewName("/role/roleList");
		return mav;
	}
	/**
	 * 跳转
	 * @param req
	 * @param code
	 * @return
	 */
	@RequestMapping("/toRoleUpdate")
	public ModelAndView toRoleUpdate(HttpServletRequest req,String code){
		ModelAndView mav = new ModelAndView();
		String flag = null;
		if(StringUtils.isEmpty(code)){
			flag = "add";
		}else{
			flag = "update";
			Role role = roleService.getRoleByCode(code);
			mav.addObject("role", role);
		}
		mav.addObject("flag", flag);
		mav.setViewName("/role/roleUpdate");
		return mav;
	}
	/**
	 * 添加或修改
	 * @param req
	 * @param user
	 * @param flag
	 * @param headerPicture
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/roleUpdate")
	public ModelAndView roleUpdate(HttpServletRequest req,Role role,String flag) throws IOException{
		ModelAndView mav = new ModelAndView();
		if(flag!=null){
			if("add".equals(flag)){
				String maxCode = roleService.getMaxCode();
				String code = null;
				if(StringUtils.isEmpty(maxCode)){
					code = "100001";
				}else{
					code = (Integer.parseInt(maxCode)+1)+"";
				}
				role.setCode(code);
				role.setState(0);
				roleService.addRole(role);
			}else{
				roleService.updateRoleByCode(role);
			}
		}
		mav.setViewName("redirect:/role/roleList");
		return mav;
	}
	/**
	 * 删除
	 * @param req
	 * @param code
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteRole")
	public Map<String,String> deleteRole(HttpServletRequest req,String code){
		Map<String,String> map = new HashMap<String,String>();
		if(!StringUtils.isEmpty(code)){
			roleService.deleteRoleByCode(code);
			map.put("result", "删除成功！");
		}else{
			map.put("result", "参数不能为空！");
		}
		return map;
	}
}
